import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from 'vuex-persist'

const vuexLocal = new VuexPersistence({
  storage: window.sessionStorage
})

Vue.use(Vuex);

const store = new Vuex.Store({
  /**
   * 单一状态data
   */
  state: {
    fullLoading: 0,// 0-未占用、1在占用
    broswer: '',// 浏览器信息
    curIndex: 0,// 当前通用头部菜单索引
    IPv4: '1.0.0.0',// ip地址
    mac: '',// mac地址
    game: {
      info: {// 大赛信息
        ID: '',
      },
      detail: {}// 奖项信息
    },
    designer: {
      loginID: '',
      designerID: '',
      isJoin: '',// 报名资格 0无，1有
      isAdmin:''//是否管理员 0 否， 1是
    },
    screenWidth: document.body.clientWidth + 5,// 屏幕尺寸
    tags: [],
    preGames: []
  },
  /**
   * computed
   */
  getters: {
    gameID(state) {
      return state.game.info.ID || '';
    }
  },
  /**
   * sync
   * methods
   */
  mutations: {

    /**
     * TODO:
     * - 全局loading尚未实现
     */
    setFullLoading(state, fullLoading) {
      state.fullLoading = fullLoading;
    },



    setDesigner(state, dsg) {
      state.designer[dsg.key] = dsg.val;
    },
    setMac(state, mac) {
      state.mac = mac;
    },
    setBrowser(state, broswer) {
      state.broswer = broswer;
    },
    setPreGames(state, preGames) {
      state.preGames = preGames;
    },
    setDesignerLoginID(state, loginID) {
      state.designer.loginID = loginID;
    },
    setDesignerJoin(state, join) {
      state.designer.isJoin = join;
    },
    setDesignerAdmin(state, admin) {
      state.designer.isAadmin = admin;
    },
    setCurIndex(state, index) {
      state.curIndex = index;
    },
    setIPv4(state, ip) {
      state.IPv4 = ip;
    },
    setGameInfo(state, info) {
      state.game.info = info;
    },
    setGameDetail(state, detail) {
      state.game.detail = detail;
    },
    setTags(state, tags) {
      state.tags = tags;
    },
    setDesignerID(state, designerID) {
      state.designer.designerID = designerID;
    },
    removeDesignerID(state) {
      state.designer.designerID = '';
    },
    removeDesigner(state) {
      for(const key in state.designer) {
        state.designer[key] = '';
      }
    },
    resizeScreen(state, width) {
      // 5px是右边滚动轮的宽度
      state.screenWidth = width + 5;
    },
    clearTags(state) {
      // state.tags.forEach(tag => {
      //   tag.sub.forEach(sub => {
      //     Vue.set(sub, 'color', void 0);
      //   });
      // });
    }
  },
  /**
   * async
   * methods
   */
  actions: {

  },
  plugins: [vuexLocal.plugin]
})

export default store